package controller

import (
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"html/template"
	"net/http"
	"strconv"
	"time"
)

/**
Admin301自动跳转(无等待时间)
*/
func AdminIndexHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == true {
		dao.RedirectPage(w, "网页跳转提示", "已登录", "/admin/home", 200, 0)
	} else {
		dao.RedirectPage(w, "网页跳转提示", "未登录", "/admin/user/login", 401, 0)
	}
	return
}

/**
显示登陆页面
*/
func AdminUserLoginHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == true {
		dao.RedirectPage(w, "Error", "已登录", "/admin/home", 200, 3)
		return
	}

	t, err := template.New("login.html").Funcs(dao.FuncMap).ParseFiles("templates/login.html", "templates/pub_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**
管理员登录
*/
func DoAdminUserLoginHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	G := new(dao.GuestSession)
	//检查是否未登录
	if G.Init(r).CheckIsLogin() == true {
		dao.RedirectPage(w, "Error", "已登录", "/admin/home", 200, 3)
		return
	}

	//账户查询
	var U dao.AdminUser
	err := U.CheckLogin(r.PostFormValue("userName"), r.PostFormValue("userPass"))
	if err != nil {
		dao.RedirectPage(w, "登录失败", err.Error(), "/admin/user/login", 500, 3)
		return
	}

	//用户状态查询
	if U.Status == -1 {
		dao.LogError(err)
		dao.RedirectPage(w, "Error", "用户被禁用", "/admin/user/login", 403, 3)
		return
	}

	//记录登陆日志
	var AL dao.AdminLogin
	AL.Uid = U.Id
	AL.Key = G.Key
	AL.Status = 1
	AL.LoginFrom = 1
	AL.LoginIp = dao.GetCurrentIp(r)
	AL.UserAgent = dao.GetCurrentUA(r)
	AL.AddTime = time.Now().Format(dao.TimeLayout)
	err = AL.Add()
	if err != nil {
		dao.LogError(err)
		dao.RedirectPage(w, "Error", "记录登陆日志失败"+err.Error(), "/admin/user/login", 500, 3)
		return
	}

	//写入Session
	G.Uid, G.Name, G.NiceName, G.Email, G.Phone, G.FaceImg, G.Status, G.Description, G.LastLoginIp, G.LastLoginTime, G.LoginFrom, G.UserAgent =
		U.Id, U.Name, U.NiceName, U.Email, U.Phone, U.FaceImg, U.Status, U.Description, AL.LoginIp, AL.AddTime, AL.LoginFrom, AL.UserAgent
	err = G.SetSession()
	if err != nil {
		dao.LogError(err)
		dao.RedirectPage(w, "Error", "写入Session失败:"+err.Error(), "/admin", 500, 3)
		return
	}

	//写入Cookie
	cookie := http.Cookie{Name: "guestId", Value: G.Key, HttpOnly: false, Expires: time.Now().AddDate(1, 0, 0), Path: "/"}
	w.Header().Set("Set-Cookie", cookie.String())

	//自动跳转
	dao.RedirectPage(w, "Success", "登录成功", "/admin/home", 200, 3)
}

/**
管理员退出登录
*/
func AdminUserLogoutHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//清除Session
	err := guest.Delete()
	if err != nil {
		dao.LogError(err)
		dao.RedirectPage(w, "Error", "Session写入失败", "/admin/home", 503, 3)
		return
	}

	//修改状态
	var AL dao.AdminLogin
	AL.Logout(guest.GetGuestId())
	dao.RedirectPage(w, "Success", "退出登陆", "/admin", 200, 3)
}

/**
管理员登录后首页
*/
func AdminHomeHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminHome.html").Funcs(dao.FuncMap).ParseFiles("templates/adminHome.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminSearchHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminSearch.html").Funcs(dao.FuncMap).ParseFiles("templates/adminSearch.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminPostsHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminPost.html").Funcs(dao.FuncMap).ParseFiles("templates/adminPost.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

//admin - 获取文章详情
func AdminPostsInfoHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.SendResult(w, dao.ERR_NO_LOGIN.Code, dao.ERR_NO_LOGIN.Message, nil)
		return
	}

	id, err := strconv.ParseInt(ps.ByName("id"), 10, 64)
	if err != nil {
		dao.SendResult(w, dao.ERR_POST_ID.Code, dao.ERR_POST_ID.Message, nil)
		return
	}

	info, err := dao.GetPostInfo(id, false)
	if err != nil {
		dao.SendResult(w, dao.ERR_POST_NOT_FIND.Code, dao.ERR_POST_NOT_FIND.Message, nil)
		return
	}

	dao.SendResult(w, dao.ERR_NONE.Code, dao.ERR_NONE.Message, info)
}

/**

 */
func AdminSuggestHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminSuggest.html").Funcs(dao.FuncMap).ParseFiles("templates/adminSuggest.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminAdminsHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminAdmins.html").Funcs(dao.FuncMap).ParseFiles("templates/adminAdmins.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminVersionHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminVersion.html").Funcs(dao.FuncMap).ParseFiles("templates/adminVersion.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminBackupHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminBackup.html").Funcs(dao.FuncMap).ParseFiles("templates/adminBackup.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminAccountHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminAccount.html").Funcs(dao.FuncMap).ParseFiles("templates/adminAccount.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminAboutHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminAbout.html").Funcs(dao.FuncMap).ParseFiles("templates/adminAbout.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminSiteOptHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminSiteOpt.html").Funcs(dao.FuncMap).ParseFiles("templates/adminSiteOpt.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}

/**

 */
func AdminSettingHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	guest := new(dao.GuestSession)
	//检查是否未登录
	if guest.Init(r).CheckIsLogin() == false {
		dao.RedirectPage(w, "Error", "未登录", "/admin/user/login", 503, 3)
		return
	}

	//渲染页面
	t, err := template.New("adminSetting.html").Funcs(dao.FuncMap).ParseFiles("templates/adminSetting.html", "templates/pub_inc.html", "templates/admin_inc.html")
	if err != nil {
		dao.LogError(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	t.Execute(w, nil)
}
